<?php
/**
 *抽奖
 * wsl
 * 2022/7/17
 */
namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Service\LotteryService;
use App\Models\DrawRecords;
use App\Models\RafflePrizes;
use App\Repository\RepositoryInterface\LotteryInterface;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class LotteryController extends Controller
{
    //
    protected $lottery;

    public function __construct(LotteryInterface $lottery){
        $this->lottery = $lottery;
    }

    /**
     * @return mixed
     * 查询可抽商品
     */
    public function LotteryGoods()
    {
        try {
            //查询可抽商品
            $lottery_goods = $this->lottery->lotteryGoods();
            return $lottery_goods;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @return mixed
     * 后台查询可抽商品
     */
    public function LotteryGood()
    {
        try {
            //查询可抽商品
            $lottery_goods = $this->lottery->lotteryGoods();
//            return $lottery_goods;
            return ['code' => 20000 , 'msg' => 'success' , 'data' => $lottery_goods];
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @return array
     *抽奖
     */
    public function Lottery(Request $request)
    {
        try {
            $user_id = $request->input('user_id');
            //查询可抽商品
            $lottery_goods = $this->lottery->lotteryGoods();
            //抽奖
            return LotteryService::Lottery($lottery_goods,$user_id);
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param Request $request
     * @return array|string
     * 抽奖记录查询
     */
    public function drawRecordsQuery(Request $request)
    {
        try {
            $user_id = $request->post('user_id');
            $draw_records_query = $this->lottery->drawRecordsQuery($user_id);
            return ['code'=>20000,'msg'=>'success','data'=>$draw_records_query];
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param Request $request
     * @return array
     * 奖品删除
     */
    public function drawRecordsDel(Request $request){
        try {
            $id = $request->input('id');
            $id = (int)$id;
            $draw_records_reg = RafflePrizes::where('id',$id)->delete();
            if ($draw_records_reg){
                return ['code'=>20000,'data'=>'删除成功','msg'=>'success'];
            }else{
                return ['code'=>5000,'data'=>'删除失败','msg'=>'error'];
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    public function add(Request $request){
        $data = $request->input();
        return ['code'=>20000,'data'=>$data,'msg'=>''];
    }
}
